<?php
class Users extends Zend_Db_Table_Abstract
{
    protected $_name = 'users';
    protected $_primary = 'user_id';
    /**
     * Define si el usuario activo dentro del sistema
     * @var int
     */
    const USER_ACTIVE = 1;
	public function findId( $value ) 
	{
		$row = $this->fetchRow( 
			$this->select()
				->from( $this->_name, array( 'user_id'))
				->where( "username = ?", $value )
				->orWhere( "mail = ?", $value )	
			);
		if( count( $row )) {
			return $row->user_id;
		} else {
			return false;
		}
		
	}
	/**
	 * Chech if user and pass is ok
	 */
	public function isValid( $username, $password )
	{
		$query = $this->select()
			->where( 'username = ?', $username )
			->where( 'password = ?', $this->tokenizer( $password ));
		$row = $this->fetchRow( $query );
		if( count ($row )) {
			if( $row->active == 0 ) {
				return MA_Error_User::getInstance()->get('NOT_ACTIVE');				
			}
			return $row;
		} else {
			return MA_Error_User::getInstance()->get('USER_UNKNOW');
		}
	}
	public function tokenizer( $password )
	{
		return ( string ) md5 ( $password );
	}
}